package marksys.free.com.marksys.repo;

import android.arch.lifecycle.LiveData;
import android.graphics.Bitmap;

import java.util.List;

import marksys.free.com.marksys.nettool.common.Resource;
import marksys.free.com.marksys.nettool.common.ResourceConvertUtils;
import marksys.free.com.marksys.nettool.retrofit.FaceRetrofitUtils;
import marksys.free.com.marksys.nettool.vo.searchFace;
import marksys.free.com.marksys.utils.DateUtils;
import marksys.free.com.marksys.utils.ImageUtils;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class FaceRepo {

    private static FaceRepo INSTANCE;

    public static FaceRepo newInstance() {
        if (null == INSTANCE) {
            INSTANCE = new FaceRepo();
        }
        return INSTANCE;
    }

    public LiveData<Resource<searchFace>> searchFaceStudent(List<Bitmap> image) {
        MultipartBody.Builder builder = new MultipartBody.Builder();
        for (Bitmap bitmap : image) {
            RequestBody requestBody = RequestBody.create(MediaType.parse("image/jpeg"), ImageUtils.bitmapToBytes(bitmap));
            builder.addFormDataPart("file", DateUtils.getDataStrForFormat("yyyyMMddHHmmss") + ".jpg", requestBody);
        }
        builder.setType(MultipartBody.FORM);
        return ResourceConvertUtils.convertToResource(FaceRetrofitUtils.getInstance().apiService.searchFaceStudent(builder.build()));
    }
}

